package com.laxcen.controller2;


import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.config.MyLog;
import com.laxcen.smoke.dto.req.*;
import com.laxcen.smoke.dto.res.RoleListRes;
import com.laxcen.smoke.service.RolesService;
import com.laxcen.smoke.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/roles")
public class RolesController {

    @Autowired
    private RolesService rolesService;

    @ApiOperation(value = "添加角色",notes =
            "请求信息： \n" +
                    "&emsp; name &nbsp; : &nbsp; 角色名 \n" +
                    "&emsp; description &nbsp; : &nbsp; 角色描述 \n" +
                    "&emsp; available &nbsp; : &nbsp; 是否可用  \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/insert")
    @MyLog(operation = "添加角色")
    @RequiresPermissions("role:add")
    public Response insert(@RequestBody AddRoleReq addRoleReq  ){
        return rolesService.insert(addRoleReq.getName(),addRoleReq.getDescription(),addRoleReq.getAvailable());
    }

    @ApiOperation(value = "修改角色",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 角色id \n" +
                    "&emsp; name &nbsp; : &nbsp; 角色名 \n" +
                    "&emsp; description &nbsp; : &nbsp; 角色描述 \n" +
                    "&emsp; available &nbsp; : &nbsp; 是否可用  \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/update")
    @MyLog(operation = "修改角色")
    @RequiresPermissions("role:edit")
    public Response update(@RequestBody UpdateRoleReq updateRoleReq  ){
        return rolesService.update(updateRoleReq.getId(),updateRoleReq.getName(),updateRoleReq.getDescription(),updateRoleReq.getAvailable());
    }

    @ApiOperation(value = "删除角色",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 角色id \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除角色")
    @RequiresPermissions("role:delete")
    public Response delete(@RequestBody CommonOneLongIdReq commonOneLongIdReq  ){
        return rolesService.delete(commonOneLongIdReq.getId());
    }

    @ApiOperation(value = "批量删除角色",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 角色id(多个用英文,隔开) \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/deletemore")
    @MyLog(operation = "批量删除角色")
    @RequiresPermissions("role:batchDelete")
    public Response deletemore(@RequestBody CommonOneIdReq commonOneIdReq  ){
        return rolesService.deletemore(commonOneIdReq.getId());
    }

    @ApiOperation(value = "获取角色列表",notes =
            "请求信息： \n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
            "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 角色id \n" +
                    "&emsp; name &nbsp; : &nbsp; 角色名\n" +
                    "&emsp; description &nbsp; : &nbsp; 角色描述\n" +
                    "&emsp; available &nbsp; : &nbsp; 是否可用\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/get")
    @RequiresPermissions("roles")
    public Response<PageInfo<RoleListRes>> get(@RequestBody PageInfoReq pageInfoReq  ){

        if (pageInfoReq.getPageNo()==null){
            pageInfoReq.setPageNo(1);
        }
        if (pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageSize(10);
        }
        if (pageInfoReq.getPageNo()==null&&pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageNo(1);
            pageInfoReq.setPageSize(10);
        }
        return rolesService.get(pageInfoReq.getPageNo(),pageInfoReq.getPageSize());
    }


    @ApiOperation(value = "分配资源",notes =
            "请求信息： \n" +
                    "&emsp; roleid &nbsp; : &nbsp; 角色id \n" +
                    "&emsp; resourceid &nbsp; : &nbsp; 资源id(多个资源id用英文,隔开) \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/distribute")
    @MyLog(operation = "分配资源")
    @RequiresPermissions("role:allotResource")
    public Response distribute(@RequestBody DistributeRoleReq distributeRoleReq  ){
        return rolesService.distribute(distributeRoleReq.getRoleid(),distributeRoleReq.getResourceid());
    }

}
